Welcome back.
In this lecture we're going to be checking to see if our bullets hit any limbs.
And if they do, we'll display a hit marker around the crosshair of our mouse.
So as an example here, if I go and I shoot this character right here and I click, as you can see,
a little hit marker appeared around my crosshair.
And if I get a headshot, as you can see it displays a red hit marker.
And if I continue to shoot these guys, as you can see, I'm getting hit markers on my mouse.
Back inside of the local script for our guns.
What we want to do is we want to go ahead and create a dedicated function for displaying a hit marker
on the screen, and we want to pass to this function the result from our raycast.
And then this function can go ahead and check to see if we hit anything.
And if the thing we hit belonged to a character model and it's not a player's character model.
And then from that point it can get the instance that was hit, check to see if it was a head, and
then we can display a red hit marker.
Otherwise we can display a normal white hit marker.
So for example, we would call this function from our shoot function if we were shooting a standard
gun.
After we perform the raycast, we could go ahead and call our display, hit marker function and pass
our result.
And then from this point we would go in here and check to see if we actually had a raycast result.
If we did not have a raycast result, then we can simply return.
Otherwise we can go ahead and see if this was a character model by getting the result dot instance dot
parent.
And then if inside of the parent of this instance we do not find first child which is a humanoid, then
we can also return.
Otherwise, we also want to verify that this character does not belong to a player in our game.
So if players get player from character and we pass the character, if we actually get a result from
this function, then we're just going to return.
Otherwise, we can go ahead and play the hit marker sound and display the hit marker on our screen.
So we would create a new sound here.
So we'll create sound.
And that sound is going to be parented to we could do the workspace since we want to play it globally
around the entire headphone of our player.
We don't want it to be a point in the map.
We would just want it to play normally as a sound for our player.
So we'll parent it to the workspace and then the sound is going to be the properties dot hit marker
sound properties.
Then we can go ahead and play the sound.
And then when the sound ends we can go ahead and delete it.
So sound destroy.
And then from this point, we could go ahead and check to see if the instance inside of the result we
hit is a head.
So if result dot instance dot name is equal to head, then we want to go ahead and update our hit marker
to be a red color.
So if you remember inside of each one of our guys we have an image label here called hit marker.
So that means we can go ahead and modify the image color.
So we'll refer to our guy.
And we'll go ahead and get the hip marker, and we can update the image color three to a new color three
from RGB.
And we want it to be a red color.
So we could just slide this up here.
And then there's our red color.
We'll hit okay.
Otherwise, if we did not hit a head, we can go ahead and set the geo hit marker.
Image color three back to be white.
So color three from RGB and that's just going to be pure white.
There we go.
And then when we can go ahead and do is we can go ahead and make sure the hit marker is visible.
So we'll set visible equal to true.
And then we'll delay a function for a very short period of time.
We could do like 0.1 seconds or maybe a little bit less like 0.08 seconds.
And all this function is going to do is set guy hit marker dot visible back to false.
So now we're displaying the hit marker on the screen for a very short period of time.
But the next problem we need to go ahead and solve is we want to make sure that the position of this
hit marker on our screen is equal to the exact same position of our mouse on the screen.
So what we can do for that is there is an event inside of our mouse object called move, and it says
fired when the mouse is moved.
So we can listen to this event.
And every single time this event fires, we can update the position of our hit marker to be equal to
the position of our mouse.
So what I want to do is I only want to listen to this event when I have my gun equipped, and then I
don't want to listen to the event when I have the gun unequipped.
So we can go ahead and make a variable up here.
I'm going to call this mouse connection, and this is going to store a connection to that event.
And that way when we unequip our gun we can go ahead and disconnect that connection.
Then we can go down to our equipped event.
And inside of here we can go ahead and listen to the mouse move event, connect a function to this.
And this is where we're going to update the position of that hit marker.
And then we can store this connection inside of our mouse connection variable.
And inside of here, all we're going to do is set guy hitmarker dot position equal to a new Utum two.
And this is going to be from offset.
Any position is going to be very simple.
Inside of our mouse object, we can get the x and the y position on the screen.
So we'll pass mouse dot x and then mouse dot y just like that.
And then when we go and unequip our gun.
We can go ahead and refer to mouse connection and disconnect it.
Okay, cool.
Now, the next important thing we need to do is we need to call our display hit marker function.
Also from when we're shooting a shotgun.
However, this one's going to be a little bit more involved because shotguns can have multiple pellets
coming out of them.
And what that means is some pellets might hit the head of a character model, while some might hit the
body.
If any of the pellets hit a head, then I want that one to take precedence and display the hit marker
as red, while if none of the pellets hit the head but some hit the body, then we can make it a white
hit marker.
So what I'm going to do actually, is I want to loop through every single pellet result inside of our
all pellets table.
And we're going to go ahead and check to see if any of the names of the instances we hit is equal to
the name of head.
So if pellet result dot raycast result, we get the instance and the name of that instance if it's equal
to head.
Then that means we can go ahead and just display a hit marker.
And we're going to pass the pelt result raycast result.
And then we're just going to break out of this loop.
So this will allow us to display a red hit marker.
Now, the next thing we want to do is we want to listen to see if we got any body shots on our character
model.
So what we can do is we can check to see if the pelt result dot raycast result dot instance dot parent.
We find first child which is a humanoid.
If that's the case, then that means we can go ahead and display a hit marker or a white hit marker
on our screen.
Now here's a problem.
We're looping through all of the results inside of the table.
Let's say the first result was a body shot.
Well, if we just display a white hit marker and then break out of this loop and display no other hit
markers, well, we only went through the first result.
There could have been a possibility where maybe the ninth result in our all pellets table was actually
a head shot.
So what we want to do is we actually want to create a table, and I'm going to call it body shots.
And we're going to store in here all of the pellets that hit the body of a character model.
So if we've confirmed that the name of an instance we hit was not equal to head.
Then what we can go ahead and do is we can check to see if it's still belong to a character model,
and if it did, we can insert that result into our body shots table.
So body shots and the value we want to insert is going to be pellet result dot raycast result.
Then when we're finally done looping what we can go ahead and do is we want to check to see if we got
any body shots.
So if the number of body shots is greater than zero, then that means we can go ahead and display a
hit marker and we can just pass something like the first result in body shots.
Now there's another issue here.
Let's say we loop through this table here, and we find a couple of them that are body shots.
And we insert those into the table.
But then let's say like the fourth result was a head shot.
And then we display that hit marker and then break out of this loop.
Well, once we break out of this loop we've just displayed a red hit marker.
And then since the body shots table still has stuff in it, we're going to display another hit marker.
So we don't want to display two hit markers at the same time.
So an easy way to fix that is we can check to see that once we actually got a head shot of something,
then we can simply just set the body shots table to be an empty table break out of the loop.
And that way we don't display another hit marker on the screen.
And just like that, we should be complete with displaying hit markers on the screen.
So let's go ahead and copy the assault rifle into the starter pack.
And then I'm also going to copy a shotgun in here into the starter pack.
And then I'll make sure to copy that updated local script inside of my shotgun.
And then let's go ahead and playtest our game.
So here I've got my assault rifle.
If I shoot the ground, obviously I'm not going to get any hit marker, but let's say I shoot the hand
of this character model if I shoot it.
As you can see, it displayed the hit marker and I got the hit marker sound if I get a headshot, as
you can see, the hit marker turned red and it also played the hit marker sound.
Now let's say I had a shotgun, and let's say half of the pellets hit the face and the other half hit
the body.
So if I shoot like right here, as you can see, I still got a red hit marker, even though half of
my pellets hit the torso of the player.
Now, if I only hit the torso of the player and I shoot again.
As you can see, I'm only getting a white hit marker instead of a red hit marker.
But if I hit the head, as you can see, I'm getting a red hit marker.
Alrighty, that's all I have for you in this lecture and I'll see you in the next one.